package com.wjtc.web.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wjtc.common.base.restful.Page;
import com.wjtc.common.base.restful.RtMsg;
import com.wjtc.common.base.restful.enums.RtCodeType;
import com.wjtc.common.consts.AppConsts;
import com.wjtc.common.utils.JsonUtil;
import com.wjtc.common.utils.LogUtil;
import com.wjtc.common.utils.UidUtil;
import com.wjtc.common.utils.enums.LogType;
import com.wjtc.common.utils.validate.util.ValidateUtil;
import com.wjtc.service.entity.QuestionResult;
import com.wjtc.service.inter.QuestionService;
import com.wjtc.web.model.EchartModel;
import com.wjtc.web.model.QuestionOneModel;
import com.wjtc.web.model.QuestionPageModel;
import com.wjtc.web.model.QuestionTwoModel;

// 问卷控制层
@Controller
@RequestMapping("/question")
public class QuestionController {

	@Resource
	private QuestionService questionService;

	// 毕业生调查页面
	@RequestMapping("/question")
	public String questionPage(){
		return "question/question";
	}

	// 用人单位调查页面
	@RequestMapping("/question2")
	public String questionPage2(){
		return "question/question2";
	}

	// 成功页面
	@RequestMapping("/success")
	public String success(){
		return "question/success";
	}

	// 毕业生调查页面
	@RequestMapping("/statistic")
	public String statistic(){
		return "question/statistic";
	}
	
	@RequestMapping(value = "/queryList", produces = { AppConsts.PRODUCES_APPLICATION_JSON })
	public @ResponseBody Object queryList(@RequestBody QuestionPageModel model) throws Exception {

		model.castQueryObject(model);
		Page page = questionService.queryAllListByPage(model);
		return page;
	}
	
	@RequestMapping(value = "/queryEchart")
	public @ResponseBody Object queryEchart(QuestionPageModel model) throws Exception {
		EchartModel echart = new EchartModel();
		List<Map<String, Object>> list = questionService.queryAllList(model);
		for(Map<String, Object> map : list){
			String q1 = (String)map.get("question_1");
			List<String> l = JsonUtil.toObject(q1);
			for(String q : l){
				if(q.equals("A")){
					echart.getQuestion1().add(0, echart.getQuestion1().get(0)+1);
					echart.getQuestion1().remove(1);
				}else if(q.equals("B")){
					echart.getQuestion1().add(1, echart.getQuestion1().get(1)+1);
					echart.getQuestion1().remove(2);
				}else if(q.equals("C")){
					echart.getQuestion1().add(2, echart.getQuestion1().get(2)+1);
					echart.getQuestion1().remove(3);
				}else if(q.equals("D")){
					echart.getQuestion1().add(3, echart.getQuestion1().get(3)+1);
					echart.getQuestion1().remove(4);
				}
			}
			
			String q2 = (String)map.get("question_2");
			List<String> l2 = JsonUtil.toObject(q2);
			for(String q : l2){
				if(q.equals("A")){
					echart.getQuestion2().add(0, echart.getQuestion2().get(0)+1);
					echart.getQuestion2().remove(1);
				}else if(q.equals("B")){
					echart.getQuestion2().add(1, echart.getQuestion2().get(1)+1);
					echart.getQuestion2().remove(2);
				}else if(q.equals("C")){
					echart.getQuestion2().add(2, echart.getQuestion2().get(2)+1);
					echart.getQuestion2().remove(3);
				}else if(q.equals("D")){
					echart.getQuestion2().add(3, echart.getQuestion2().get(3)+1);
					echart.getQuestion2().remove(4);
				}else if(q.equals("E")){
					echart.getQuestion2().add(4, echart.getQuestion2().get(4)+1);
					echart.getQuestion2().remove(5);
				}else if(q.equals("F")){
					echart.getQuestion2().add(5, echart.getQuestion2().get(5)+1);
					echart.getQuestion2().remove(6);
				}
			}
			
			String q3 = (String)map.get("question_3");
			List<String> l3 = JsonUtil.toObject(q3);
			for(String q : l3){
				if(q.equals("A")){
					echart.getQuestion3().add(0, echart.getQuestion3().get(0)+1);
					echart.getQuestion3().remove(1);
				}else if(q.equals("B")){
					echart.getQuestion3().add(1, echart.getQuestion3().get(1)+1);
					echart.getQuestion3().remove(2);
				}else if(q.equals("C")){
					echart.getQuestion3().add(2, echart.getQuestion3().get(2)+1);
					echart.getQuestion3().remove(3);
				}else if(q.equals("D")){
					echart.getQuestion3().add(3, echart.getQuestion3().get(3)+1);
					echart.getQuestion3().remove(4);
				}else if(q.equals("E")){
					echart.getQuestion3().add(4, echart.getQuestion3().get(4)+1);
					echart.getQuestion3().remove(5);
				}
			}
			
			
			String q4 = (String)map.get("question_4");
			List<String> l4 = JsonUtil.toObject(q4);
			for(String q : l4){
				if(q.equals("A")){
					echart.getQuestion4().add(0, echart.getQuestion4().get(0)+1);
					echart.getQuestion4().remove(1);
				}else if(q.equals("B")){
					echart.getQuestion4().add(1, echart.getQuestion4().get(1)+1);
					echart.getQuestion4().remove(2);
				}else if(q.equals("C")){
					echart.getQuestion4().add(2, echart.getQuestion4().get(2)+1);
					echart.getQuestion4().remove(3);
				}else if(q.equals("D")){
					echart.getQuestion4().add(3, echart.getQuestion4().get(3)+1);
					echart.getQuestion4().remove(4);
				}else if(q.equals("E")){
					echart.getQuestion4().add(4, echart.getQuestion4().get(4)+1);
					echart.getQuestion4().remove(5);
				}else if(q.equals("F")){
					echart.getQuestion4().add(5, echart.getQuestion4().get(5)+1);
					echart.getQuestion4().remove(6);
				}else if(q.equals("G")){
					echart.getQuestion4().add(6, echart.getQuestion4().get(6)+1);
					echart.getQuestion4().remove(7);
				}else if(q.equals("H")){
					echart.getQuestion4().add(7, echart.getQuestion4().get(7)+1);
					echart.getQuestion4().remove(8);
				}
			}
			
			String q5 = (String)map.get("question_5");
			List<String> l5 = JsonUtil.toObject(q5);
			for(String q : l5){
				if(q.equals("A")){
					echart.getQuestion5().add(0, echart.getQuestion5().get(0)+1);
					echart.getQuestion5().remove(1);
				}else if(q.equals("B")){
					echart.getQuestion5().add(1, echart.getQuestion5().get(1)+1);
					echart.getQuestion5().remove(2);
				}else if(q.equals("C")){
					echart.getQuestion5().add(2, echart.getQuestion5().get(2)+1);
					echart.getQuestion5().remove(3);
				}else if(q.equals("D")){
					echart.getQuestion5().add(3, echart.getQuestion5().get(3)+1);
					echart.getQuestion5().remove(4);
				}else if(q.equals("E")){
					echart.getQuestion5().add(4, echart.getQuestion5().get(4)+1);
					echart.getQuestion5().remove(5);
				}else if(q.equals("F")){
					echart.getQuestion5().add(5, echart.getQuestion5().get(5)+1);
					echart.getQuestion5().remove(6);
				}else if(q.equals("G")){
					echart.getQuestion5().add(6, echart.getQuestion5().get(6)+1);
					echart.getQuestion5().remove(7);
				}else if(q.equals("H")){
					echart.getQuestion5().add(7, echart.getQuestion5().get(7)+1);
					echart.getQuestion5().remove(8);
				}
			}
		}
		System.out.println(JsonUtil.toJSONStringJackson(echart));
		return echart;
	}
	/**
	 * 保存学生问卷信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/saveQuestion1")
	@ResponseBody
	public  Object saveQuestion1(QuestionOneModel acceptModel) throws Exception{
		LogUtil.LOG(LogType.INFO, JsonUtil.toJSONStringJackson(acceptModel));
		// 校验数据信息
		String error = ValidateUtil.validateSingleError(acceptModel);
		if(error!=null && !error.trim().equals("")){
			LogUtil.LOG(LogType.INFO, JsonUtil.toJSONStringJackson(new RtMsg().setCode(RtCodeType.VALIDATE).setMsg(error)));
			return new RtMsg().setCode(RtCodeType.VALIDATE).setMsg(error);
		}
		QuestionResult  result = new QuestionResult();
		result.setId(UidUtil.uuid32());
		result.setQuestion1(acceptModel.getQuestion1().toString());
		result.setQuestion2(acceptModel.getQuestion1().toString());
		result.setQuestion3(acceptModel.getQuestion1().toString());
		result.setQuestion4(acceptModel.getQuestion1().toString());
		result.setQuestion5(acceptModel.getQuestion1().toString());

		questionService.saveQuestion(result);
		LogUtil.LOG(LogType.INFO, JsonUtil.toJSONStringJackson(new RtMsg()));
		return new RtMsg();
	}

	/**
	 * 保存外部人员问卷信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/saveQuestion2")
	public @ResponseBody Object saveQuestion2(QuestionTwoModel acceptModel) throws Exception{
		LogUtil.LOG(LogType.INFO, JsonUtil.toJSONStringJackson(acceptModel));
		// 校验数据信息
		String error = ValidateUtil.validateSingleError(acceptModel);
		if(error!=null && !error.trim().equals("")){
			return new RtMsg().setCode(RtCodeType.VALIDATE).setMsg(error);
		}
		System.out.println(JsonUtil.toJSONStringJackson(acceptModel));
		return new RtMsg();
	}


}
